//https://leetcode.cn/problems/max-area-of-island/
class Solution {
    int[] dx = {1, -1, 0, 0};
    int[] dy = {0, 0, 1, -1};
    boolean[][] check;
    int ret;
    int m, n;
    int count;
    public int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        check = new boolean[m][n];
        for(int i = 0; i < m ; i++) {
            for(int j = 0; j < n; j++) {
                if(check[i][j] == false && grid[i][j] == 1) {
                    count = 0;
                    check[i][j] = true;
                    dfs(grid, i, j);//统计一块陆地的面积
                    ret = Math.max(ret, count);
                }
            }
        }
        return ret;
    }
    public void dfs(int[][] grid, int i, int j) {
        count++;       
        for(int k = 0; k < 4; k++) {
            int x = i + dx[k];
            int y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !check[x][y]) {
                check[x][y] = true;
                dfs(grid, x, y);
            }
        }
    }
}